package com.sc.controller.vehicle;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.domain.VehiclePosition;
import com.sc.dto.VehiclePositionPageDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.vehicle.IVehiclePositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/vehiclePosition")
@Api(tags = "车位管理")
@CrossOrigin
public class VehiclePositionController {
    @Autowired
    private IVehiclePositionService ivps;
    /**
     * 添加车位
     * @param vehiclePosition
     * @return
     */
    @PostMapping("/PositionAdd")
    @ApiOperation("添加车位")
    public Result add(@RequestBody VehiclePosition vehiclePosition){
        System.out.println("Received vehicle position: " + vehiclePosition);
        ivps.insertPosition(vehiclePosition);
        return Result.success();
    }
    /**
     * 查询车位详情
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation("查询车位详情")
    public Result<VehiclePosition> selectById(Integer id){
        VehiclePosition pos = ivps.getById(id);
        return Result.success(pos);
    }

    /**
     * 修改车位
     * @param vehiclePosition
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改车位")
    public Result update(@RequestBody VehiclePosition vehiclePosition){
        ivps.updateById(vehiclePosition);
        return Result.success();
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @PutMapping("/examinePass")
    @ApiOperation("审核通过")
    public Result examinePass(Integer id){
        VehiclePosition vehiclePosition = new VehiclePosition();
        vehiclePosition.setId(id);

        ivps.updateById(vehiclePosition);
        return Result.success();
    }
    /**
     * 审核未通过
     * @param id
     * @return
     */
    @PutMapping("/examineNotPass")
    @ApiOperation("审核未通过")
    public Result examineNotPass(Integer id){
        VehiclePosition vehiclePosition = new VehiclePosition();
        vehiclePosition.setId(id);

        ivps.updateById(vehiclePosition);
        return Result.success();
    }

    /**
     * 分页查询车位列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询车位列表")
    public PageResult list(Integer pageNum, Integer pageSize,String region,Integer status){
        Page<VehiclePosition> page = ivps.showRegions(pageNum, pageSize,region,status);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @GetMapping("/listNo")
    @ApiOperation("查询车位名称列表")
    public Result<List<String>> listNo(){
        List<VehiclePosition> list = ivps.list();
        List<String> nameList = list.stream()
                .map(p -> p.getNo())
                .collect(Collectors.toList());
        return Result.success(nameList);
    }

    /**
     * 删除车位
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除车位")
    public Result delete(Integer id){
        ivps.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteByIds")
    @ApiOperation("批量删除")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        ivps.removeByIds(ids);
        return Result.success();
    }

    /**
     * 条件分页查询
     * @return
     */
    @GetMapping("/listByConditions")
    @ApiOperation("条件分页查询")
    public PageResult listByConditions(VehiclePositionPageDTO pageDTO){
        Page<VehiclePosition> page=ivps.listByStatusAndNo(pageDTO);
        return new PageResult(page.getTotal(), page.getRecords());
    }
}
